package com.example.yiparkapplication.activity.index.activity.ConvenientPaymentActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.example.yiparkapplication.R;
import com.example.yiparkapplication.constant.API;
import com.example.yiparkapplication.constant.Constant;
import com.example.yiparkapplication.entity.AbnormalSingleCar;
import com.example.yiparkapplication.util.BaseActivity;
import com.example.yiparkapplication.util.Common;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//AppCompatActivity
public class ExceptionDeclareActivity extends BaseActivity implements View.OnClickListener {
    private String mLicensePlate;
    private String mSingleCarID;
    private String licensePlate;
    private int abnormalType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exception_declare_layout);

        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        mLicensePlate = pref.getString("licensePlate", "");
        Intent intent = getIntent();
        mSingleCarID = intent.getStringExtra("singleCarID");
        licensePlate = intent.getStringExtra("licensePlate");
        TextView vSingleCarID = findViewById(R.id.id_single_car_id);
        vSingleCarID.setText(mSingleCarID);

        RadioGroup radioGroup = findViewById(R.id.id_abnormal_type);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup radioGroup1, int i){
                if (i == R.id.id_abnormal_charge){
                    abnormalType = 0;
                }
                if (i == R.id.id_abnormal_timing){
                    abnormalType = 1;
                }
                if (i == R.id.id_abnormal_other){
                    abnormalType = 2;
                }
            }
        });
        Button submitAbnormal = findViewById(R.id.id_submit_abnormal);
        submitAbnormal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.id_submit_abnormal:
                if ("".equals(mLicensePlate)){
                    Toast.makeText(getApplicationContext(), Constant.THE_USER_NO_LICENSE_PLAT, Toast.LENGTH_LONG).show();
                    return;
                }
                EditText vAbnormalDescription = findViewById(R.id.id_abnormal_description);
                String abnormalDescription = vAbnormalDescription.getText().toString();
                if (abnormalType == 2){
                    if ("".equals(abnormalDescription)){
                        Toast.makeText(getApplicationContext(), "异常描述不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                AbnormalSingleCar abnormalSingleCar = new AbnormalSingleCar();
                abnormalSingleCar.setAbnormalSingleCarSingleCarId(Integer.valueOf(mSingleCarID));
                abnormalSingleCar.setAbnormalSingleCarType(abnormalType);
                abnormalSingleCar.setAbnormalSingleCarUpDetails(abnormalDescription);
                abnormalSingleCar.setAbnormalSingleCarLicenseplate(licensePlate);
                reportErrorSingleCar(abnormalSingleCar);
                break;
        }
    }

    private void reportErrorSingleCar(AbnormalSingleCar abnormalSingleCar){
        OkHttpClient okHttpClient = new OkHttpClient();
        String json = JSONObject.toJSONString(abnormalSingleCar);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder()
                .url(API.HOST + API.URL_REPORT_ERROR_SINGLE_CAR)
                .post(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e){
                Common.failureMessage(e.getMessage(), getApplicationContext());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response)throws IOException{
                String tempResponse = response.body().string();
                JSONObject joOut = Common.tempResponseException(tempResponse, getApplicationContext());
                int code = joOut.getIntValue("code");
                if (code == 100){
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(), "申报成功", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }else{
                    Looper.prepare();
                    Toast.makeText(getApplicationContext(), joOut.getString("msg"), Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }
        });
    }
}
